//
//  HHNewsCell.m
//  HHBroker
//
//  Created by He Wei on 10/3/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHNewsCell.h"
#import "UIViewAdditions.h"
#import "UIImageView+WebCache.h"
#import "WMString-Utilities.h"
#import "HHLineView.h"

#define KHeightOfLittleNews 49
#define KTagOfLittleNewsButton 2000

@interface LittleNewsView : UIView

@property (nonatomic, strong) UIImageView *newsImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *newsButton;

@end

@implementation LittleNewsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.newsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.width-10-39, 5, 39, 39)];
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, self.width-10-39-50, 39)];
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.textColor = UIColorFromRGB(0x2d2d2d);
        self.newsButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.newsButton.frame = CGRectMake(0, 0, self.width, 49);
        [self.newsButton setBackgroundImage:[UIImage imageNamed:@"button_bg_highlighted"] forState:UIControlStateHighlighted];
        HHLineView *line = [HHLineView lineViewWithY:0];
        [self addSubview:self.newsImageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.newsButton];
        [self addSubview:line];
        self.clipsToBounds = YES;
        
    }

    return self;
}
@end

@interface HHNewsCell ()

@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIImageView *newsImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *littleNewsBackView;

@end

@implementation HHNewsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //time
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.frame = CGRectMake((kCurrentWidth-100)/2.0, 15, 100, 15);
        self.timeLabel.layer.masksToBounds = YES;
        self.timeLabel.layer.cornerRadius = 4;
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.backgroundColor = UIColorFromRGB(0xcecece);
        self.timeLabel.textColor = [UIColor whiteColor];
        self.timeLabel.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:self.timeLabel];
        
        //background
        self.backView = [[UIView alloc]init];
        self.backView.frame = CGRectMake(15, self.timeLabel.bottom+15, kCurrentWidth-30, 175);
        self.backView.backgroundColor = [UIColor whiteColor];
        self.backView.layer.cornerRadius = 5;
        self.backView.layer.borderColor = UIColorFromRGB(0xffffff).CGColor;
        self.backView.layer.borderWidth = 1;
        [self.contentView addSubview:self.backView];
        
        //newsImage
        self.newsImageView = [[UIImageView alloc] init];
        self.newsImageView.frame = CGRectMake(10, 10, self.backView.width-20, 150);
        self.newsImageView.userInteractionEnabled = YES;  //图片可点击
        [self.backView addSubview:self.newsImageView];
        
        //添加button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = self.newsImageView.bounds;
        [button setBackgroundImage:[UIImage imageNamed:@"button_bg_highlighted"] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(bigNewsButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.newsImageView addSubview:button];
        
        //newsTitle
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.frame = CGRectMake(0, self.newsImageView.height-40, self.newsImageView.width, 40);
        self.titleLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [self.newsImageView addSubview:self.titleLabel];
        
        //littleNews
        self.littleNewsBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 175, self.backView.width, 0)];
        [self.backView addSubview:self.littleNewsBackView];
    }

    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setNewsArray:(NSArray *)newsArray
{
    if (newsArray.count == 0) {
        return;
    }
    _newsArray = newsArray;
    HHNewsModel *model = newsArray[0];
    NSURL *url = [NSURL URLWithString:model.img_url];
    [self.newsImageView sd_setImageWithURL:url];
    self.titleLabel.text = model.news_title;
    self.timeLabel.text = [NSString changeTheDateString:model.release_time];
    [self.timeLabel sizeToFit];
    self.timeLabel.width += 10;
    self.timeLabel.left = (kCurrentWidth-self.timeLabel.width)/2.0;
    
    [self.littleNewsBackView removeAllSubviews];
    self.backView.height = 175+(newsArray.count-1)*KHeightOfLittleNews;
    self.littleNewsBackView.height = (newsArray.count-1)*KHeightOfLittleNews;
    
    if (newsArray.count == 1) {
        return;
    }
    // 绘制littleViews
    for (int i=1; i<newsArray.count; i++) {
        HHNewsModel *model = newsArray[i];
        CGRect frame = CGRectMake(0, (i-1)*KHeightOfLittleNews, self.littleNewsBackView.width, KHeightOfLittleNews);
        LittleNewsView *littleNews = [[LittleNewsView alloc] initWithFrame:frame];
        littleNews.titleLabel.text = model.news_title;
        NSURL *url = [NSURL URLWithString:model.img_url];
        [littleNews.newsImageView sd_setImageWithURL:url];
        [self.littleNewsBackView addSubview:littleNews];
        // button
        littleNews.newsButton.tag = KTagOfLittleNewsButton+i;
        [littleNews.newsButton addTarget:self action:@selector(newsButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }

}

- (void)newsButtonClicked:(UIButton *)button
{
    NSInteger index = button.tag - KTagOfLittleNewsButton;
    // 当 index == 0 为 big news
    if (index >= self.newsArray.count) {
        return;
    }
    // bigNewsButtonClicked:
    HHNewsModel *model = self.newsArray[0];
    if (self.delegate && [self.delegate respondsToSelector:@selector(newsListCellDidClickedWithNewsID:)]) {
        [self.delegate newsListCellDidClickedWithNewsID:model.news_id];
    }
}


- (void)bigNewsButtonClicked:(UIButton *)button
{
    // bigNewsButtonClicked:
    HHNewsModel *model = self.newsArray[0];
    if (self.delegate && [self.delegate respondsToSelector:@selector(newsListCellDidClickedWithNewsID:)]) {
        [self.delegate newsListCellDidClickedWithNewsID:model.news_id];
    }
}

+ (CGFloat)heightForModelCount:(NSInteger)count
{
    if (count == 1) {
        return 220;
    } else {
        return 220 + (count-1)*KHeightOfLittleNews;
    }
}


@end
